<?php
// users (id, name, gender, age)
// 1. 全部查询  select * from users
// 2. 部分字段查询  select name, age from users
// 3. 带条件查询  select * from users where age >= 30 and age <=50
// 4. 排序 select * from users where ... order by id desc
// 5. 限制条目 select * from users where ... limit 30, 10 order by id desc;

// 1. 利用PDO连接数据库，实例化一个PDO对象
$dsn = 'mysql:host=localhost;dbname=phpcode20;charset=utf8';
$pdo = new PDO($dsn, 'root', '123456');
$pdo->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);

// 两个常用的方法，
// (1)exec() 执行数据库的写操作 增insert 删delete 改update
// (2)query() 执行数据库的读操作 查 select
// 2. 准备sql语句
$sql = 'select * from users order by id desc';
// 3. 执行sql语句
$result = $pdo->query($sql); // PDOStatement
$result = $result->fetchAll();
?>
<html>
<head>
    <title>账户列表</title>
</head>
<body>
<table width="100%" border="1" cellpadding="0" cellspacing="0">
    <tr>
        <th>编号</th>
        <th>姓名</th>
        <th>联系方式</th>
        <th>账户余额</th>
    </tr>
    <?php foreach ($result as $val):?>
    <tr>
        <td><?php echo $val['id'];?></td>
        <td><?php echo $val['name'];?></td>
        <td><?php echo $val['mobile'];?></td>
        <td><?php echo $val['money'];?></td>
    </tr>
    <?php endforeach;?>
</table>
</body>
</html>
